/**  
* @Title: UserServiceImpl.java
* @Package com.house.service.impl.user
* @version V1.0  
*/ 
package com.hjb.service.impl.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hjb.service.config.rest.RestClient;
import com.hjb.service.domain.Member;
import com.hjb.service.mapper.MemberMapper;
import com.hjb.service.mapper.RebateDetailMapper;
import com.hjb.service.user.UserService;

import tk.mybatis.mapper.entity.Example;

/**
* @ClassName: UserServiceImpl
* @Description: TODO(用户中心)
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>
* @date 2017年5月6日 下午12:44:25
*/
@Service
public class UserServiceImpl implements UserService {

	private Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private MemberMapper memberMapper;

	@Autowired
	private RebateDetailMapper rebateDetailMapper;
	
	@Autowired
	private RestClient restClient;
	/* (non-Javadoc)
	 * @see com.house.service.user.UserService#getUserInfo(java.lang.String)
	 */
	@Override
	public Member getUserInfo(String openId) {
		
		Member member = new Member();
		member.setId(openId);
		
		return memberMapper.selectByPrimaryKey(member);

	}
	@Override
	public void createUser(Member member) {
		
		memberMapper.insertSelective(member);
		
	}
	@Override
	public String getUserFromWx(String userId) {
		String url="https://api.weixin.qq.com/sns/userinfo?access_token=";
		
		Member member = new Member();
		member.setId(userId);
		member = memberMapper.selectByPrimaryKey(member);
		url+=(member.getUserAccessToken()+"&openid="+userId+"&lang=zh_CN");
		log.info("从微信获取用户信息url========"+url);
		String result = restClient.get(url, new HashMap<>(), String.class);
		log.info("微信返回用户信息======"+result);
		return result;
	}
	@Override
	public void updateToken(Member member) {
		
		memberMapper.updateByPrimaryKeySelective(member);
		
	}
	@Override
	public int supplement(HttpServletRequest request) {
		Member member = new Member();
		member.setId(request.getSession().getAttribute("userId")+"");
//		member.setId("2017051322170858073337");
		if(request.getParameter("name")!=null && !request.getParameter("name").equals(""))member.setName(request.getParameter("name"));
		if(request.getParameter("mobile")!=null && !request.getParameter("mobile").equals(""))member.setMobileNo(request.getParameter("mobile"));
		if(request.getParameter("mail")!=null && !request.getParameter("mail").equals(""))member.setEmail(request.getParameter("mail"));
		if(request.getParameter("bankNo")!=null && !request.getParameter("bankNo").equals(""))member.setBankNo(request.getParameter("bankNo"));
		if(request.getParameter("bankName")!=null && !request.getParameter("bankName").equals(""))member.setBankName(request.getParameter("bankName"));
		return memberMapper.updateByPrimaryKeySelective(member);
	}
	@Override
	public List<Member> getUserbyInviteCode(String inviteCode) {
		return null;
	}
	@Override
	public List<Member> getUserInfoByOpendId(String string) {
		Example example = new Example(Member.class);
		example.createCriteria().andEqualTo("wxOpenId",string);
		return memberMapper.selectByExample(example);
	}
	@Override
	public List<Map<String,Object>> getInterest(String userId) {
		log.info("获取用户推荐人列表,userId===="+userId);
		List<Map<String,Object>> list = rebateDetailMapper.getInterest(userId);
		log.info("获取用户推荐人列表,返回===="+list);
		return list;
	}
	@Override
	public String getInterestCount(String userId) {
		Map<String, Object> map = rebateDetailMapper.getInterestCount(userId);
		log.info("获取用户返利总金额："+(map==null?"没查出来":map.get("count")));
		if(map !=null && map.get("count")!=null){
			return map.get("count")+"";
		}

		return "0.00";
	}

}
